<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="资产编号" clearable></el-input>
      </el-form-item>


      <template>
        <el-select v-model="dataForm.key2" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.key2"
            :label="item.label"
            :value="item.key2">
          </el-option>
        </el-select>
      </template>

      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('assets:repairs:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('assets:repairs:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
<!--      <el-table-column
        prop="repairsId"
        header-align="center"
        align="center"
        label="序号">
      </el-table-column>-->

      <el-table-column
        prop="assetsName"
        header-align="center"
        align="center"
        label="资产名字">
      </el-table-column>
      <el-table-column
        prop="assetsNum"
        header-align="center"
        align="center"
        label="资产编号">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="报修人">
      </el-table-column>
      <el-table-column
        prop="repairsTime"
        header-align="center"
        align="center"
        label="报修日期">
      </el-table-column>
      <el-table-column
        prop="repairsStatus"
        header-align="center"
        align="center"
        label="维修状态">  <!-- 0维修完成 1:维修 2:报废 -->
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.repairsStatus == 0">审核中/报修</el-tag>
          <el-tag type="success" v-if="scope.row.repairsStatus == 1">维修完成</el-tag>
          <el-tag type="warning" v-if="scope.row.repairsStatus == 2">维修中</el-tag>
          <el-tag type="danger" v-if="scope.row.repairsStatus == 3">报废</el-tag>
          <el-tag type="danger" v-if="scope.row.repairsStatus == 4">不同意/维修</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="repairsPic"
        header-align="center"
        align="center"
        label="报修附件">
        <template slot-scope="scope">
          <img :src=scope.row.repairsPic  style="width: 150px;height: 150px;">
        </template>
      </el-table-column>
      <el-table-column
        prop="repairsDesc"
        header-align="center"
        align="center"
        label="维修说明">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('assets:repairs:repairsOk')" type="text" size="small" @click="repairsOkHandle(scope.row.repairsId)" :disabled="scope.row.repairsStatus!=2">维修完成</el-button>
          <el-button v-if="isAuth('assets:repairs:scrapAdd')" type="text" size="small" @click="scrapAddHandle(scope.row.repairsId)" :disabled="scope.row.repairsStatus!=2">报废</el-button>
          <el-button v-if="isAuth('assets:repairs:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.repairsId)">修改</el-button>
          <el-button v-if="isAuth('assets:repairs:delete')" type="text" size="small" @click="deleteHandle(scope.row.repairsId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './repairs-add-or-update'
  export default {
    data () {
      return {
        options: [{
          key2: '',
          label: '全部'
        }, {
          key2: '0',
          label: '维修申请中'
        }, {
          key2: '1',
          label: '维修完成'
        }, {
          key2: '2',
          label: '维修中'
        }, {
          key2: '3',
          label: '报废'
        }, {
          key2: '4',
          label: '报修失败'
        }],
        dataForm: {
          key: '',
          key2: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/assets/repairs/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'key2': this.dataForm.key2,
            'userId': this.$store.state.user.id, // 传回id判断角色
            'userName': this.$store.state.user.name  // 传回名字
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //  获得当前时间
      getCurrentTime () {
        return new Date()
      },
      // 报废
      scrapAddHandle (id) {
        const selectedRow = this.dataList.find(row => row.repairsId === id)   // 点击拿到维修表这一行的ID
        if (selectedRow && selectedRow.repairsStatus === '2') {   // 是维修中才能点击
          this.$http({
            url: this.$http.adornUrl('/assets/repairs/scrapAdd'),
            method: 'post',
            data: this.$http.adornData({
              'scrapTime': this.getCurrentTime(),  // 报废时间
              'userName': selectedRow.userName,
              'assetsNum': selectedRow.assetsNum,
              'assetsName': selectedRow.assetsName
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          this.$message({
            message: '该资产不能报废',
            type: 'warning'
          })
        }
      },
      // 维修完成
      repairsOkHandle (id) {
        const selectedRow = this.dataList.find(row => row.repairsId === id)   // 点击审核拿到这一行的ID
        if (selectedRow && selectedRow.repairsStatus === '2') {   // 是维修中才能点击
          this.$http({
            url: this.$http.adornUrl('/assets/repairs/repairsOk'),
            method: 'post',
            data: this.$http.adornData({
              'repairsId': selectedRow.repairsId,
              'repairsTime': selectedRow.repairsTime,
              'userName': selectedRow.userName,
              'userMobile': selectedRow.userMobile,
              'repairsStatus': selectedRow.repairsStatus,
              'assetsNum': selectedRow.assetsNum,
              'assetsName': selectedRow.assetsName,
              'repairsDesc': selectedRow.repairsDesc,
              'repairsPic': selectedRow.repairsPic
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          this.$message({
            message: '该资产不用维修',
            type: 'warning'
          })
        }
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.repairsId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/assets/repairs/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
